<?php

class Article {

    public $id;
    public $title;
    public $abstract;
    public $content;
    public $image;
    public $author;
    public $links;
    public $tags;
    public $stuffs;
    public $categories;
    public $date;
    public $time;
    public $creator;

    public function get_seo_link() {
        $seo = "articoli/";
        $array = explode(" ", self::escape_string($this->title));
        foreach ($array as $value) {
            if ($value != "")
                $seo .= $value . '-';
        }

        $seo .= $this->id . ".html";
        return strtolower($seo);
    }

    public static function escape_string($string) {
        $escaping_chars = array("à", "á", "è", "é", "ì", "í", "ò", "ó", "ù", "ú", "/", "°", ",", ".", ":", ";", "?", "!", "-", "\n", "'", "+", "(", ")", '"');
        $replacing_chars = array("a", "a", "e", "e", "i", "i", "o", "o", "u", "u", " ", "", "", "", "", "", "", "", "", " ", " ", " ", " ", " ");
        return str_replace($escaping_chars, $replacing_chars, $string);
    }

    public function get_html_article() {
        ?>
        <article>
            <h1> <?= $this->title ?></h1>
            <section id="article">
                <div class="span3">
                    <img src="<?= ($this->image) ? '../images/' . $this->image : 'http://placehold.it/250x250' ?>" alt="" class="img-polaroid"/>
                </div> 
                <?= $this->content ?>
                <?php if ($this->author != "") { ?>
                    <dl class="dl-horizontal">
                        <dt>Autore:</dt>
                        <dd><?= $this->author ?></dd>
                    </dl>
                <?php } ?>
            </section>
            <section id="links">
                <h2>Link Utili</h2>
                <?= self::get_links_list($this->links) ?> 
            </section>
            <section id="stuffs">
                <h2>Materiale</h2>
                <?= self::get_stuffs_list($this->stuffs) ?>
            </section>
            <section id="tags">
                <h2>Tags</h2>
                <?= self::get_tags_list($this->tags) ?>
            </section>
        </article>

        <?php
    }

    public function get_html_article_preview() {
        ?>
        <div class="row">
            <div class="span2 image">
                <img src="<?= ($this->image) ? 'http://www.salesianichioggia.it/images/' . $this->image : 'http://placehold.it/160x160' ?>" alt="" class="img-polaroid"/>
            </div>
            <div class="span6">
                <h2><?= $this->title ?></h2>
                <p><i class="icon-time"></i> <strong><?= self::get_preview_article_date($this->date->format('Y-m-d')) ?></strong></p> 
                <blockquote><p><?= $this->abstract ?></p></blockquote>
                <dl class="dl-horizontal">
                    <dt>Categoria:</dt>
                    <dd>
                        <?php
                        foreach ($this->categories as $category) {
                            ?>
                            <a href="http://www.salesianichioggia.it/articoli/archivio/categorie/<?= $category ?>""><span class="label label-inverse"><?= $category ?></span></a> 
                            <?php
                        }
                        ?>
                    </dd>
                    <dt>Tags:</dt>
                    <dd>
                        <?php
                        if (count($this->tags) > 0) {
                            foreach ($this->tags as $tag) {
                                ?>
                                <a href="#"><span class="label label-info"><?= $tag ?></span></a> 
                                <?php
                            }
                        } else {
                            ?>
                            <p>Nessun tag associato all'articolo</p>
                            <?php
                        }
                        ?>
                    </dd>
                </dl>
                <a class="btn btn-info btn-small" href="http://www.salesianichioggia.it/<?= $this->get_seo_link() ?>">Leggi l'articolo</a>
            </div>
        </div>

        <?php
    }

    public function get_dashboard_list_html() {
        ?>
        <tr>
            <td><img src="http://localhost:8888/salesianichioggia.it/images/<?= $this->image ?>" alt=""></td>
            <td><?= $this->title ?></td>
            <td><?= $this->abstract ?></td>
            <td><?= $this->date->format("d/m/Y") ?></td>
            <td>
                <div class="btn-group">
                    <a class="btn" href="http://localhost:888/salesianichioggia.it/dashboard/article.php?action=edit&id=<?= $this->id ?>"><i class="icon-pencil"></i></a>
                    <a class="btn"><i class="icon-eye-open"></i></a>
                    <a class="btn btn-danger" href="http://localhost:888/salesianichioggia.it/dashboard/article.php?action=delete&id=<?= $this->id ?>"><i class="icon-trash"></i></a>
                </div>
            </td>
        </tr>
        <?php
    }
    
    public function get_javascript_tag_array() {
        
    }
    
    private static function get_links_list($links) {
        ?>
        <?php if (count($links) == 0) { ?>
            <p>Nessun Link</p>
        <?php } else {
            ?>
            <ul>
                <?php
                foreach ($links as $link) {
                    ?>
                    <li><a href="<?= $link ?>"><?= $link ?></a></li>
                    <?php
                }
                ?>
            </ul>
            <?php
        }
    }

    private static function get_stuffs_list($stuffs) {
        ?>
        <?php if (count($stuffs) == 0) { ?>
            <p>Nessun Materiale</p>
        <?php } else {
            ?>
            <ul>
                <?php
                foreach ($stuffs as $stuff) {
                    ?>
                    <li><a href="<?= $stuff ?>"><?= $stuff ?></a></li>
                    <?php
                }
                ?>
            </ul>
            <?php
        }
    }

    private static function get_tags_list($tags) {
        ?>
        <p>
            <?php if (count($tags) == 0) { ?>
                Nessun tag
                <?php
            } else {
                foreach ($tags as $tag) {
                    ?>
                    <a href="#"><span class="label label-info"><?= $tag ?></span></a> 
                    <?php
                }
            }
            ?>
        </p>
        <?php
    }

    private static function get_preview_article_date($date_str) {
        list($year, $month, $day) = explode("-", $date_str);
        $months = array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
        return $day . " " . $months[$month - 1] . " " . $year;
    }

}
?>